package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhouwei
 * @date 2020-01-09 16:06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    //注入加密对象
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        if(username==null){
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
        System.out.println(user.getUsername());
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user);
    }

    /**
     * 修改密码
     * @param formData
     * @return
     */
    @RequestMapping("/changecode")
    public Result changecode(@RequestBody Map<String,String> formData){
        //校验原密码
        //获取后台数据库密文密码
        com.itheima.pojo.User user = userService.findByUsername(formData.get("username"));
        String password = user.getPassword();
        //用户输入的原密码
        String fcode=formData.get("oldcode");
        //对比
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        boolean matches = bc.matches(fcode,password);
        try {
            //如果密码真确，就将新密码加密修改
            if(matches){
                //调用md加密，
                String md5code = passwordEncoder.encode(formData.get("newcode"));
                formData.put("md5code",md5code);
                userService.changecode(formData);
            }else{
                return new Result(false,"原密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"当前服务器正忙");
        }
        return new Result(true,"密码修改成功，请重新登录");
    }

    /**
     * 查询用户看到的菜单
     * @param username
     * @return
     */
    @RequestMapping("/getMenu")
    public Result getMenu(String username){
        List<Map> list= null;
        try {
            list = userService.findMenu(username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
        return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
    }
}
